package com.ac.driver.mqtt.subscriber;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

/**
 * @ClassName : MqttSystemHandler
 * @Description : 监听系统信息
 * @Author : Alan
 * @Date: 2020-09-14 15:21
 */
@Slf4j
@Component
public class MqttSystemHandler implements MessageHandler {


    @ServiceActivator(inputChannel = "topic-system")
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {

        log.info("系统主题消息:{}",message);

        JSONObject payload = JSONObject.parseObject((String) message.getPayload());
        MessageHeaders headers =  message.getHeaders();

        String  mqttReceivedTopic = headers.get("mqtt_receivedTopic")+"";
        String command = mqttReceivedTopic.substring(mqttReceivedTopic.lastIndexOf("/")+1);
        String clientId = payload.getString("clientid");

        if(clientId.contains("_ip")){
            log.info("物管平台设备接入,clientId:{}",clientId);
            return;
        }

        switch (command){
            //case "connected":{connectedHandler(clientId,payload,headers);break;}
            //case "disconnected":{disconnectedHandler(clientId);break;}
            default:{
                log.error("暂不支持该命令:{}",command);
            }
        }

    }



}
